/**
 * created by 小卷毛, 2019-09-20 020
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.aifeng.utils

import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.nio.channels.FileChannel
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

object MD5 {

    private val hexDigits = charArrayOf(
        '0', '1', '2', '3',
        '4', '5', '6', '7',
        '8', '9', 'a', 'b',
        'c', 'd', 'e', 'f'
    )

    private fun toHexString(bytes: ByteArray?): String {
        if (bytes == null) return ""
        val hex = StringBuilder(bytes.size * 2)
        bytes.forEach {
            hex.append(hexDigits[it.toInt() shr 4 and 0x0F])
            hex.append(hexDigits[it.toInt() and 0x0F])
        }
        return hex.toString()
    }

    @Throws(IOException::class)
    fun md5(file: File): String {
        var stream: FileInputStream? = null
        var channel: FileChannel? = null
        try {
            val messagedigest = MessageDigest.getInstance("MD5")
            stream = FileInputStream(file)
            channel = stream.channel
            val byteBuffer = channel!!.map(FileChannel.MapMode.READ_ONLY, 0, file.length())
            messagedigest.update(byteBuffer)

            return toHexString(messagedigest.digest())
        } catch (neverHappened: NoSuchAlgorithmException) {
            throw RuntimeException(neverHappened)
        } finally {
            stream?.close()
            channel?.close()
        }
    }

    fun md5(string: String): String {
        try {
            val encodeBytes =
                MessageDigest.getInstance("MD5")
                    .digest(string.toByteArray(charset("UTF-8")))
            return toHexString(encodeBytes)
        } catch (neverHappened: NoSuchAlgorithmException) {
            throw RuntimeException(neverHappened)
        } catch (neverHappened: UnsupportedEncodingException) {
            throw RuntimeException(neverHappened)
        }
    }

}